package com.itheima.service.Impl.question;

import com.github.pagehelper.PageHelper;
import com.itheima.domain.Catalog;
import com.itheima.domain.Company;
import com.itheima.domain.Dept;
import com.itheima.domain.Question;
import com.itheima.mapper.store.catalog.CatalogMapper;
import com.itheima.mapper.store.company.CompanyMapper;
import com.itheima.mapper.store.question.QuestionMapper;
import com.itheima.mapper.system.dept.DeptMapper;
import com.itheima.service.QuestionService;
import com.itheima.utils.IDutils;
import com.itheima.utils.MapperUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.poi.util.StringUtil;

import java.util.Date;
import java.util.List;

public class QuestuonServiceImp implements QuestionService {
    @Override
    public List<Question> findAll(String page, String pageSize) {
        SqlSession session = MapperUtil.getSession();
        QuestionMapper mapper = session.getMapper(QuestionMapper.class);
        int page11=1;
        int pageSize22=10;
        if(StringUtils.isNotBlank(page)){
            page11 = Integer.parseInt(page);
        }
        if (StringUtils.isNotBlank(pageSize)){
            pageSize22=Integer.parseInt(pageSize);
        }
        PageHelper.startPage(page11,pageSize22);
        List<Question> list=  mapper.findAll();
        MapperUtil.close(session);
        return list;
    }

    @Override
    public List<Company> findAll1() {
        SqlSession session = MapperUtil.getSession();
        CompanyMapper mapper = session.getMapper(CompanyMapper.class);


        List<Company> companyList= mapper.findAll();
        MapperUtil.close(session);
        return companyList;
    }

    @Override
    public List<Catalog> findAll2() {
        SqlSession session = MapperUtil.getSession();
        CatalogMapper mapper = session.getMapper(CatalogMapper.class);

        List<Catalog> list= mapper.findAll();
        MapperUtil.close(session);
        return list;
    }

    @Override
    public String save(Question question,boolean flag) {
        SqlSession session = MapperUtil.getSession();
        QuestionMapper mapper = session.getMapper(QuestionMapper.class);
      String id1=IDutils.getUUId();
        question.setId(id1);
        question.setCreateTime(new Date());
        if(flag) {
            question.setPicture(id1);
        }
        mapper.save(question);



        session.commit();
        MapperUtil.close(session);

        return id1;

    }

    @Override
    public Question findById(String id) {
        SqlSession session = MapperUtil.getSession();
        QuestionMapper mapper = session.getMapper(QuestionMapper.class);
        Question question=mapper.findById(id);
        MapperUtil.close(session);
        return question;

    }

    @Override
    public void edit(Question question,boolean flag) {
        SqlSession session = MapperUtil.getSession();
        QuestionMapper mapper = session.getMapper(QuestionMapper.class);

        if(flag) {
            question.setPicture(question.getId());
        }
        mapper.edit(question);

        session.commit();
        MapperUtil.close(session);


    }

    @Override
    public void delete(String ids) {
        SqlSession session = MapperUtil.getSession();
        QuestionMapper mapper = session.getMapper(QuestionMapper.class);
        String[] id1 = ids.split(",");
        mapper.delete(id1);
        session.commit();
        MapperUtil.close(session);
    }
}
